<?php


namespace app\live\controller\v1;


use app\common\cachemodel\live\LiveAnchorInfo;
use app\common\cachemodel\live\LiveRecordingInfo;
use app\common\lib\Util;
use app\live\controller\LiveAuth;
use think\Env;

class LiveInfo extends LiveAuth
{

    /**
     * 把app里的header里的内容通过参数传到webview里
     */
    public function redirectUrl()
    {
        $headers = request()->header();
        $params = request()->param();
        $this->redirect(Util::getZhunleWapLiverRedirectUrl($headers, $params));
    }

    /**
     * 获取主播信息
     */
    public function getLiveAnchorInfo()
    {
        $recordId = (new LiveRecordingInfo())->getNewestRecord($this->liveInfo['live_id']);
        $record = (new LiveRecordingInfo())->getInfoById($recordId);

        return show(config('code.success'), '请求成功', [
            'avatar' => $this->liveInfo['avatar'],
            'nickname' => $this->liveInfo['name'],
            'intro' => $this->liveInfo['intro'] ?? '',
            'id' => 'ID：' . $this->liveInfo['live_id'] ?? '',
            'status' => (int)($record['status'] ?? 0) === 1 ? 1 : 0
        ]);
    }

    /**
     * 获取直播记录
     */
    public function recordList()
    {
        $record = (new LiveRecordingInfo())->recordList($this->liveInfo['live_id']);

        foreach ($record['data'] as $k => $v)
        {
            $record['data'][$k]['link'] = 'liver_page?page=live_detail&id=' . $v['id'];
        }
        return show(config('code.success'), '请求成功', $record);
    }

    public function updateInfo()
    {
        $type = input('type/s', '');
        $value = input('value/s', '');

        if (!in_array($type, ['avatar', 'name', 'intro', 'beauty_config']))
        {
            return show(config('code.error'), '请求异常，请稍后再试～');
        }
        $data = [];

        switch ($type)
        {
            case 'avatar':
                if (empty($value))
                {
                    return show(config('code.error'), '头像不能为空');
                }
                $data = ['avatar' => $value];
                break;
            case 'name':
                if (empty($value))
                {
                    return show(config('code.error'), '昵称不能为空');
                }
                if (mb_strlen($value) >= 8)
                {
                    return show(config('code.error'), '昵称不能超过8个字');
                }
                $data = ['name' => $value];
                break;
            case 'intro':
                if (mb_strlen($value) >= 60)
                {
                    return show(config('code.error'), '简介不能超过60字');
                }
                $data = ['intro' => $value];
                break;
            case 'beauty_config':
                $data = ['beauty_config' => $value];
                break;
            default:
        }

        $res = (new LiveAnchorInfo())->updateData($this->liveInfo['live_id'], $data);

        if ($res)
        {
            return show(config('code.success'), '更新成功');
        } else
        {
            return show(config('code.error'), '服务异常，请稍后再试～');
        }
    }
}
